/**
 *  *2010-7-26 下午04:41:52  add 
 */
package com.base.myproject.client.HR.holiday;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BaseObject;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.ui.BaseFormPanel;
import com.base.myproject.client.ui.BaseGridPanel;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.base.myproject.client.ui.celleditor.BaseComboBoxForm;
import com.base.myproject.client.ui.celleditor.BaseComboBoxX;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.Style.VerticalAlignment;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * 假期策略
 * 
 * @author Administrator 2011-08-09 cxghiu4102@gmail.com
 */
public class VacationManagementPloy extends BaseJDBCPanel {
	private List<Button> buttonList = new ArrayList<Button>();
	private TextArea deductionSalary = new TextArea();
	private TextArea dateTest = new TextArea();
	private TextArea fousArea = new TextArea();
	
	private BaseComboBoxX salarys = new BaseComboBoxX("desc_cn", " (select distinct hr_salary_item.desc_cn from bcode_Allowance, hr_salary_item where  no_ = code union select distinct b.desc_cn from dbo.FixedExtPloy a, hr_salary_item b where a.code = b.code) as a", "");
	
	public VacationManagementPloy() {
		init();
	}

	public void init() {
		salarys.addListener(Events.Change, getListener());
		salarys.setWidth(80);
		
		addBaseButtons();
		//addSalaryItemButtons();
		initWedgitx();
		registerMainStore(vacationManagementStore);
	}
	
	private void initWedgitx() {
		HorizontalPanel hpanel = new HorizontalPanel();
		hpanel.add(createVacationManagementGrid());
		VerticalPanel vpanel = new VerticalPanel();
		vpanel.setSpacing(1);
		hpanel.setSpacing(2);
		vpanel.add(createVacationManagementForm());

		TabPanel panel = new TabPanel();
		panel.setPlain(true);
		panel.setSize(810, 166);
		
		FieldSet conditionPrompt = new FieldSet();
		conditionPrompt.add(createConditionForm());
		conditionPrompt.setHeading("扣款键盘");
		
		vpanel.add(conditionPrompt);
		hpanel.add(vpanel);
		add(hpanel);
	}
	
	private BaseStore<BusiObject> vacationManagementStore = null;

	private BaseStore<BusiObject> getVacationManagementStore() {
		if (vacationManagementStore == null) {
			createVacationManagementStore();
		}
		return vacationManagementStore;
	}

	private BaseStore<BusiObject> createVacationManagementStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("code");
		type.getField("code").setType(String.class);
		type.addField("startDate");
		type.getField("startDate").setType(Date.class);
		type.addField("endDate");
		type.getField("endDate").setType(Date.class);
		type.addField("dateTest");
		type.getField("dateTest").setType(String.class);
		type.addField("deductionSalary");
		type.getField("deductionSalary").setType(String.class);
		type.addField("holidayCode");
		type.getField("holidayCode").setType(String.class);
		StoreInfo info = new StoreInfo();
		info.setTablename("hr_Attendance_vacationManagement");
		info.setKeyname(new String[] { "icode" });
		info.setSql("select * from hr_Attendance_vacationManagement");
		vacationManagementStore = new BaseStore<BusiObject>(type, info);
		
		return vacationManagementStore;
	}

	private BaseGridPanel vacationManagementGrid = null;

	private BaseGridPanel createVacationManagementGrid() {
		CheckBoxSelectionModel<BaseObject> cbs = new CheckBoxSelectionModel<BaseObject>();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());
		//
	
		ColumnConfig C2 = new ColumnConfig("code", "名称：", 100);
		columns.add(C2);


		ColumnModel cm = new ColumnModel(columns);
		vacationManagementGrid = new BaseGridPanel(getVacationManagementStore(), cm, true, true);
		cbs.setSelectionMode(SelectionMode.SINGLE);
		vacationManagementGrid.getBg().setSelectionModel(cbs);
		vacationManagementGrid.getBg().setHeight(460);
		return vacationManagementGrid;
	}

	private BaseFormPanel createVacationManagementForm() {
		BaseFormPanel ployForm = new BaseFormPanel(getVacationManagementStore(), false);
		TableLayout ployLayout = new TableLayout(8);
		
		TableData layout1Times1 = new TableData();
		TableData layout3Times3 = new TableData();
		TableData layout1Times3 = new TableData();
		
		layout1Times1.setRowspan(1);
		layout1Times1.setHorizontalAlign(HorizontalAlignment.LEFT);
		layout1Times1.setVerticalAlign(VerticalAlignment.TOP);
		layout1Times1.setPadding(2);
		
		layout3Times3.setColspan(3);
		layout3Times3.setRowspan(3);
		layout3Times3.setHorizontalAlign(HorizontalAlignment.LEFT);
		layout3Times3.setVerticalAlign(VerticalAlignment.TOP);
		layout3Times3.setPadding(2);
		
		layout1Times3.setColspan(1);
		layout1Times3.setRowspan(3);
		layout1Times3.setHorizontalAlign(HorizontalAlignment.LEFT);
		layout1Times3.setVerticalAlign(VerticalAlignment.TOP);
		layout1Times3.setPadding(2);
		
		ployForm.setLayout(ployLayout);
		ployForm.setHeaderVisible(false);

		ployForm.add(new Label("名称:"), layout1Times1);
		TextField<String> C_num2 = new TextField<String>();
		C_num2.setWidth(100);
		C_num2.setName("code");
		ployForm.add(C_num2, layout1Times1);
		
		ployForm.add(new Label("策略有效期从:"), layout1Times1);
		DateField C_num3 = new DateField();
		C_num3.setWidth(100);
		C_num3.getPropertyEditor().setFormat(
				DateTimeFormat.getFormat("yyyy-MM-dd"));
		C_num3.setAllowBlank(false);
		C_num3.setName("startDate");
		ployForm.add(C_num3, layout1Times1);
		
		
		ployForm.add(new Label("到:"), layout1Times1);
		DateField C_num4 = new DateField();
		C_num4.setWidth(100);
		C_num4.getPropertyEditor().setFormat(
				DateTimeFormat.getFormat("yyyy-MM-dd"));
		C_num4.setName("endDate");
		ployForm.add(C_num4, layout1Times1);
		
		ployForm.add(new Label("对应假期:"), layout1Times1);
		BaseComboBoxForm<BaseObject> holidayCode = new BaseComboBoxForm<BaseObject>("HolsCode", "HolsName", "hr_Attendance_holsType", " where 1=1", true);
		holidayCode.setAllowBlank(false);
		holidayCode.setWidth(100);
		holidayCode.setName("holidayCode");
		ployForm.add(holidayCode, layout1Times1);
		
		ployForm.add(new Label("假期天数定义:"), layout1Times3);
		dateTest.setFireChangeEventOnSetValue(true);
		dateTest.addListener(Events.Focus, getListener());
		dateTest.setWidth(280);
		dateTest.setHeight(250);
		dateTest.setName("dateTest");
		ployForm.add(dateTest, layout3Times3);
		
		ployForm.add(new Label("扣款定义:"), layout1Times3);
		deductionSalary.addListener(Events.Focus, getListener());
		deductionSalary.setFireChangeEventOnSetValue(true);
		deductionSalary.setWidth(280);
		deductionSalary.setHeight(250);
		deductionSalary.setName("deductionSalary");
		ployForm.add(deductionSalary, layout3Times3);
		
		ployForm.setWidth(810);
		ployForm.setHeight(300);
		ployForm.inited();

		return ployForm;
	}
	
	private BaseFormPanel createConditionForm() {
		BaseFormPanel conditionForm = new BaseFormPanel(new BaseStore<BaseObject>(), false);
		TableLayout conditionLayout = new TableLayout(9);
		TableData layout1Times1 = new TableData();
		
		conditionForm.setLayout(conditionLayout);
		conditionForm.setHeaderVisible(false);
		conditionForm.setBodyBorder(false);
		conditionForm.setTitle("扣款键盘");
		
		layout1Times1.setRowspan(1);
		layout1Times1.setHorizontalAlign(HorizontalAlignment.LEFT);
		layout1Times1.setVerticalAlign(VerticalAlignment.TOP);
		layout1Times1.setPadding(2);
		
		int conditionOfAddLabel = 1;
		for(Button b : buttonList) {
			if (conditionOfAddLabel == 1) {
				conditionForm.add(new Label("表达式："), layout1Times1);
			} else if (conditionOfAddLabel == 6) {
				conditionForm.add(new Label(""), layout1Times1);
				conditionForm.add(new Label(""), layout1Times1);
				conditionForm.add(new Label(""), layout1Times1);
				conditionForm.add(new Label("运算符："), layout1Times1);
			} else if (conditionOfAddLabel == 12) {
				conditionForm.add(new Label(""), layout1Times1);
				conditionForm.add(new Label(""), layout1Times1);
				conditionForm.add(new Label("字段："), layout1Times1);
			}
			conditionOfAddLabel++;
			b.setWidth(80);
			conditionForm.add(b, layout1Times1);
			b.addListener(Events.Select, getListener());
		}
		conditionForm.add(salarys, layout1Times1);
		
		conditionForm.setWidth(780);
		conditionForm.setHeight(150);

		return conditionForm;
	}
	
	private void addBaseButtons() {
		Button if1 = new Button("如果");
		Button from = new Button("从");
		Button to = new Button("到");
		Button then = new Button("则");
		Button else1 = new Button("余下");
		Button work = new Button("工作年限");
		Button dates = new Button("请假天数");
		Button moreThan = new Button("大于");
		Button lessThan = new Button("小于");
		Button or = new Button("或者");
		Button and = new Button("并且");
		Button equale = new Button("等于");
		Button notEquale = new Button("不等于");
		
		buttonList.add(from);
		buttonList.add(to);
		buttonList.add(if1);
		buttonList.add(then);
		buttonList.add(else1);
		buttonList.add(moreThan);
		buttonList.add(lessThan);
		buttonList.add(or);
		buttonList.add(and);
		buttonList.add(equale);
		buttonList.add(notEquale);
		buttonList.add(work);
		buttonList.add(dates);
	}
	
	private int sync = 0;
	private Listener<BaseEvent> defaultlistener;

	private Listener<BaseEvent> getListener() {
		if (defaultlistener != null) {
			return defaultlistener;
		}

		defaultlistener = new Listener<BaseEvent>() {
			public void handleEvent(BaseEvent be) {
				sync++;
				try {
					if (sync == 1) {
						Object source = be.getSource();
						String text = "";
						
						if (be.getType() == Events.Focus) {
							fousArea = (TextArea)source;
						} else if (be.getType() == Events.Change) {
							text = ((BaseComboBoxX)source).getRawValue();
							fousArea.setValue(fousArea.getRawValue() + " " + text + " ");
						} else {
							text = ((Button)source).getText();
							fousArea.setValue(fousArea.getRawValue() + " " + text + " ");
						}
					}
				} finally {
					sync--;
				}
			}

		};
		return defaultlistener;
	}
}
